package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author chengtaoxie
 * @since 2023-10-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    /**
     * 用户登录功能
     * @param user
     * @return
     */
    @Override
    public Result login(User user) {
        String userName = user.getUserName();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        User user1 = userMapper.selectOne(queryWrapper);
        if(user1 == null){
            return Result.fail().message("用户名不存在！");
        }
        queryWrapper.eq("pass_word",user.getPassWord());
        User user2 = userMapper.selectOne(queryWrapper);
        if(user2 == null){
            return Result.fail().message("用户输入的密码错误！");
        }
        return Result.ok().message("登录成功");
    }

    /**
     * 用户注册功能
     * @param user
     * @return
     */
    @Override
    public Result register(User user) {
        String userName = user.getUserName();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        User user1 = userMapper.selectOne(queryWrapper);
        if(user1 != null){
            return Result.fail("用户名已存在！");
        }
        userMapper.insert(user);
        return Result.ok("注册成功");
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Override
    public Result updateUser(User user) {
        userMapper.updateById(user);
        return Result.ok().message("修改成功");
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @Override
    public Result deleteUser(Integer id) {
        User user = userMapper.selectById(id);
        if(user == null){
            return Result.fail().message("用户id不存在！");
        }
        userMapper.deleteById(id);
        return Result.ok().message("删除成功！");
    }



}
